﻿using UnityEngine;
using System;
using System.Collections.Generic;

class U3DHelp
{
    //------------------------------------------------------//
    public static T FindChildInGameObject<T>(GameObject gb, string szName) where T : Component
    {
        if (gb == null)
        {
            return null;
        }
        GameObject child = FindChildInGameObject(gb, szName);
        if (child != null)
        {
            return child.GetComponent<T>();
        }
        return null;
    }
    //------------------------------------------------------//
    public static GameObject FindChildInGameObject(GameObject gb, string szName)
    {
        if (gb == null)
        {
            return null;
        }
        int nCount = gb.transform.GetChildCount();
        for (int i = 0; i < nCount; i++)
        {
            Transform t = gb.transform.GetChild(i);
            if (t.name == szName)
            {
                return t.gameObject;
            }
            else
            {
                GameObject gbFind = FindChildInGameObject(t.gameObject, szName);
                if (gbFind != null)
                {
                    return gbFind;
                }
            }
        }
        return null;
    }
    //------------------------------------------------------//
    public static GameObject FindChildInGameObject(GameObject gb, List<string> aName)
    {
        if (aName.Count == 0)
            return gb;
        string name = aName[0];
        aName.RemoveAt(0);
        if (gb == null)
        {
            return FindChildInGameObject(GameObject.Find(name), aName);
        }
        else
        {
            return FindChildInGameObject(FindChildInGameObject(gb, name), aName);
        }
    }
    //------------------------------------------------------//
    public static GameObject FindChildInGameObject(string szName)
    {
        List<string> aName = new List<string>(szName.Split('/'));
        return FindChildInGameObject(null, aName);
    }
    //------------------------------------------------------//
    public static T FindComponentInChildren<T>(GameObject gb) where T : Component
    {
        if (gb == null)
        {
            return null;
        }
        int nCount = gb.transform.GetChildCount();
        for (int i = 0; i < nCount; i++)
        {
            Transform t = gb.transform.GetChild(i);
            T comp = t.gameObject.GetComponent<T>();
            if (comp != null)
            {
                return comp;
            }
            else
            {
                T t_find = FindComponentInChildren<T>(t.gameObject);
                if (t_find != null)
                {
                    return t_find;
                }
            }
        }
        return null;
    }

    public static void ChangeGameObjectLayer(GameObject gb, int layer)
    {
        if (gb == null)
        {
            return;
        }
        gb.layer = layer;
        int nCount = gb.transform.childCount;
        for (int i = 0; i < nCount; i++)
        {
            Transform t = gb.transform.GetChild(i);
            t.gameObject.layer = layer;

            ChangeGameObjectLayer(t.gameObject, layer);
        }
    }
}